package com.xx.pmis.web.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.xx.pmis.model.OtherExpense;
import com.xx.pmis.model.TourismPlan;
import com.xx.pmis.services.OtherExpenseService;
import com.xx.pmis.services.TourismPlanService;
import com.xx.pmis.web.forms.OtherExpenseForm;
import com.xx.pmis.web.util.AccountUtil;
import com.xx.pmis.web.util.CurrentDate;
import com.xx.pmis.web.util.Rand;

/**
 * @class 其他费用Action类 <br>
 * 
 * @editor 石星星 @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class OtherExpenseAction extends PMISBaseAction {
	private static final Log log = LogFactory.getLog(OtherExpenseAction.class);

	OtherExpenseService otherExpenseService = getPMISServices().getOtherExpenseService();
	TourismPlanService tourismPlanService = getPMISServices().getTourismPlanService();

	AccountUtil accountUtil = new AccountUtil();

	// 查询所有其他费用
	public ActionForward listOtherExpense(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<OtherExpense> otherExpenseList = null;
		try {
			otherExpenseList = otherExpenseService.getAllOtherExpenseByCondition(" order by oeDate desc");
		} catch (Exception e) {
			String errorPage = "【旅游管理/其他费用】查询【其他费用】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("otherExpenseList", otherExpenseList);
		request.setAttribute("position", "旅游管理/其他费用");
		return mapping.findForward("listOtherExpense");
	}

	// 新增
	public ActionForward addOtherExpense(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<?> tourismPlanList = null;
		try {
			tourismPlanList = tourismPlanService
					.getAllTourismPlanByCondition(" where tpState = '1' order by beginDate desc");
		} catch (Exception e) {
			String errorPage = "【旅游管理/其他费用/新增】查询未完成【旅游计划】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		if (tourismPlanList.size() == 0) {
			String errorPage = "【旅游管理/其他费用/新增】没有未完成【旅游计划】，无法新增【其他费用】！";
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("date", CurrentDate.currentDate());
		request.setAttribute("tourismPlanList", tourismPlanList);
		request.setAttribute("position", "其他费用/新增");
		return mapping.findForward("addOtherExpense");
	}

	// 新增，处理方法
	public ActionForward addOtherExpense_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		OtherExpenseForm f = (OtherExpenseForm) form;

		String oeDate = f.getOeDate(); // 消费日期，消费日期在结算时作为流水账记账日期
		String oeType = f.getOeType(); // 消费类型 用餐 0， 服装 1，礼品 2，打的 3
		String oeCity = f.getOeCity(); // 所在城市
		String oeExpenses = f.getOeExpenses(); // 费用
		String oeDesc = f.getOeDesc(); // 描述
		String tpID = f.getTpID(); // 旅游计划ID

		OtherExpense otherExpense = new OtherExpense();
		otherExpense.setOeID(CurrentDate.currentYear() + Rand.getRand());
		otherExpense.setOeDate(oeDate);
		otherExpense.setOeType(oeType);
		otherExpense.setOeCity(oeCity);
		otherExpense.setOeExpenses(accountUtil.if1ZeroTo2Zero(oeExpenses));
		otherExpense.setOeDesc(oeDesc);
		otherExpense.setOePay("1");

		otherExpense.setTpID(tpID);
		try {
			otherExpenseService.saveOrUpdateOtherExpense(otherExpense);
		} catch (Exception e) {
			String errorPage = "【旅游管理/其他费用/新增】保存或更新【其他费用】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		listOtherExpenseByPage(mapping, form, request, response);
		return mapping.findForward("listOtherExpense");
	}

	// 编辑
	public ActionForward editOtherExpense(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String oeID = request.getParameter("oeID");
		String pageNum = request.getParameter("pageNum");

		OtherExpense otherExpense = null;
		try {
			otherExpense = otherExpenseService.getOtherExpenseByOtherExpenseID(oeID);
		} catch (Exception e) {
			String errorPage = "【旅游管理/其他费用/编辑】根据【其他费用ID】查询该【其他费用】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		TourismPlan tourismPlan = null;
		try {
			tourismPlan = tourismPlanService.getTourismPlanByTourismPlanID(otherExpense.getTpID());
		} catch (Exception e) {
			String errorPage = "【旅游计划/交通费/编辑】根据旅游计划ID查询该【旅游计划】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		List<TourismPlan> tourismPlanList = null;
		try {
			tourismPlanList = tourismPlanService
					.getAllTourismPlanByCondition(" where tpState = '1' order by beginDate desc");
		} catch (Exception e) {
			String errorPage = "【旅游管理/其他费用/编辑】查询未完成【旅游计划】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("pageNum", pageNum);
		request.setAttribute("tpName", tourismPlan.getTpName());
		request.setAttribute("tourismPlanList", tourismPlanList);
		request.setAttribute("otherExpense", otherExpense);
		request.setAttribute("position", "其他费用/编辑");
		return mapping.findForward("editOtherExpense");
	}

	// 编辑，处理方法
	public ActionForward editOtherExpense_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String pageNum = request.getParameter("pageNum");

		OtherExpenseForm f = (OtherExpenseForm) form;
		String oeID = f.getOeID(); // 费用ID
		String oeDate = f.getOeDate(); // 消费日期，消费日期在结算时作为流水账记账日期
		String oeType = f.getOeType(); // 消费类型 用餐 0， 服装 1，礼品 2，打的 3
		String oeCity = f.getOeCity(); // 所在城市
		String oeExpenses = f.getOeExpenses(); // 费用
		String oeDesc = f.getOeDesc(); // 描述
		// 其他费用
		OtherExpense otherExpense = null;
		try {
			otherExpense = otherExpenseService.getOtherExpenseByOtherExpenseID(oeID);
		} catch (Exception e) {
			String errorPage = "【旅游管理/其他费用/编辑】根据【其他费用ID】查询该【其他费用】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		otherExpense.setOeDate(oeDate);
		otherExpense.setOeType(oeType);
		otherExpense.setOeCity(oeCity);
		otherExpense.setOeExpenses(accountUtil.if1ZeroTo2Zero(oeExpenses));
		otherExpense.setOeDesc(oeDesc);

		try {
			otherExpenseService.saveOrUpdateOtherExpense(otherExpense);
		} catch (Exception e) {
			String errorPage = "【旅游管理/其他费用/编辑】保存或更新【其他费用】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("pageNum", pageNum);
		listOtherExpenseByPage(mapping, form, request, response);
		return mapping.findForward("listOtherExpense");
	}

	// 删除
	public ActionForward delOtherExpense(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String pageNum = request.getParameter("pageNum");

		// 其他费用
		OtherExpense otherExpense = null;
		OtherExpenseForm f = (OtherExpenseForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				otherExpense = otherExpenseService.getOtherExpenseByOtherExpenseID(ids[i]);
			} catch (Exception e) {
				String errorPage = "【旅游管理/其他费用/删除】根据【其他费用ID】查询该【其他费用】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
			try {
				otherExpenseService.deleteOtherExpense(otherExpense);
			} catch (Exception e) {
				String errorPage = "【旅游管理/其他费用/删除】保存或更新【其他费用】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
		}

		request.setAttribute("pageNum", pageNum);
		listOtherExpenseByPage(mapping, form, request, response);
		return mapping.findForward("listOtherExpense");
	}

	// 结算
	public ActionForward payOtherExpense(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String oeID = request.getParameter("oeID");
		String pageNum = request.getParameter("pageNum");

		OtherExpense otherExpense = new OtherExpense();
		try {
			otherExpense = otherExpenseService.getOtherExpenseByOtherExpenseID(oeID);
		} catch (Exception e) {
			String errorPage = "【旅游管理/其他费用/结算】根据【其他费用ID】查询该【其他费用】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		TourismPlan tourismPlan = null;
		try {
			tourismPlan = tourismPlanService.getTourismPlanByTourismPlanID(otherExpense.getTpID());
		} catch (Exception e) {
			String errorPage = "【旅游计划/住宿费/结算】根据旅游计划ID查询该【旅游计划】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		// 生成记账凭证
		// 科目代码
		String subjectCode = "";
		if (tourismPlan.getTpType().equals("0")) {
			// 旅游 0
			subjectCode = "0030005";
		} else if (tourismPlan.getTpType().equals("1")) {
			// 出差 1
			subjectCode = "0030012";
		} else {
			// 探亲 2
			subjectCode = "0030018";
		}
		accountUtil.payExpenseToAccount(otherExpense.getOeDate(), subjectCode, "1", otherExpense.getOeExpenses(),
				tourismPlan.getTpName(), "/" + otherExpense.getOeType());
		// 更新结算状态，结算完成为"0"
		otherExpense.setOePay("0");
		try {
			otherExpenseService.saveOrUpdateOtherExpense(otherExpense);
		} catch (Exception e) {
			String errorPage = "【旅游管理/其他费用/结算】保存或更新【其他费用】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("pageNum", pageNum);
		listOtherExpenseByPage(mapping, form, request, response);
		return mapping.findForward("listOtherExpense");
	}

	// 分页显示
	public ActionForward listOtherExpenseByPage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		int PAGESIZE = 20;// 每页记录数
		int totalPage = 1;// 总页数

		// 查询总用户记录数
		int count = 0;
		try {
			count = otherExpenseService.getCountOfOtherExpenseByCondition("");
		} catch (Exception e) {
			String errorPage = "【旅游管理/其他费用】查询【其他费用】数目异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("count", count);
		if (count % PAGESIZE == 0) {
			totalPage = count / PAGESIZE;
		} else {
			totalPage = count / PAGESIZE + 1;
		}
		request.setAttribute("totalPage", totalPage);
		// 声明要翻的页
		String pageNum = "1";
		try {
			String thisPageNum = request.getParameter("pageNum");
			// 如果传了页码参数
			if (thisPageNum != null && thisPageNum.length() != 0) {
				pageNum = thisPageNum;
			}
		} catch (RuntimeException e) {
			log.info("" + e.toString());
		}
		log.info("page:" + pageNum);
		request.setAttribute("pageNum", pageNum);
		List<OtherExpense> otherExpenseList = null;
		List<OtherExpense> otherExpenseListTemp = new ArrayList<OtherExpense>();
		try {
			otherExpenseList = otherExpenseService.getAllOtherExpenseByConditionByPage(" order by oeDate desc", Integer
					.parseInt(pageNum), PAGESIZE);
		} catch (Exception e) {
			String errorPage = "【旅游管理/其他费用】查询【其他费用】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		for (OtherExpense otherExpense : otherExpenseList) {
			TourismPlan tourismPlan = null;
			try {
				tourismPlan = tourismPlanService.getTourismPlanByTourismPlanID(otherExpense.getTpID());
			} catch (Exception e) {
				String errorPage = "【旅游计划/其他费用/分页显示】根据旅游计划ID查询该【旅游计划】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
			otherExpense.setTpName(tourismPlan.getTpName());
			otherExpenseListTemp.add(otherExpense);

		}
		request.setAttribute("otherExpenseList", otherExpenseList);
		request.setAttribute("PAGESIZE", PAGESIZE);
		// 如果要翻的这页是第一页
		if ("1".equals(pageNum)) {
			// 设置noUp为yes(界面上用此控制'首页'与'上一页'按钮失效)
			request.setAttribute("noUp", "yes");
		}
		// 如果要翻的这页是最后页
		if (totalPage == Integer.parseInt(pageNum)) {
			// 设置noDown为yes(界面上用此控制'下一页'与'尾页'按钮失效)
			request.setAttribute("noDown", "yes");
		}

		request.setAttribute("action", "otherExpense.do");
		request.setAttribute("position", "旅游管理/其他费用");
		return mapping.findForward("listOtherExpense");
	}

	// 明细
	public ActionForward detailOtherExpense(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String oeID = request.getParameter("oeID");

		OtherExpense otherExpense = null;
		try {
			otherExpense = otherExpenseService.getOtherExpenseByOtherExpenseID(oeID);
		} catch (Exception e) {
			String errorPage = "【旅游管理/其他费用/明细】根据【其他费用ID】查询该【其他费用】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("otherExpense", otherExpense);
		request.setAttribute("position", "其他费用/明细");
		return mapping.findForward("detailOtherExpense");
	}

}